
package com.compuvest.webstatus;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.compuvest.web.SQLBean;
// </editor-fold>

@WebServlet(name="WebStatus",loadOnStartup=1,urlPatterns={"/webinfo/webstatus"})
public class WebStatus extends HttpServlet {

    private final static Logger log = Logger.getLogger(WebStatus.class.getName());

    public SQLBean sb;

// <editor-fold defaultstate="collapsed" desc="init()">

    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

        sb = (SQLBean)config.getServletContext().getAttribute("sb");
        if(sb == null) {
            log.log(Level.SEVERE, "sb=null");
        }

        log.log(Level.INFO, "Init");
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        String id = request.getParameter("id");
        String json = "";
        if(id != null && "0d33c0c15a4838dde681654new".equals(id)) {
            WebStat webStat = sb.loadWebStats();
            json = webStat.getJSON();
        }

        response.setContentType("text/html;charset=UTF-8");
        try(PrintWriter out = response.getWriter()) {
            out.print(json);
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "WebStatus";
    }

    @Override
    public void destroy() {
        log.log(Level.INFO, "Destroy");
    }
// </editor-fold>
}
